package com.kgd.news.controller.admin;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.ehcache.CacheKit;
import com.jfinal.upload.UploadFile;
import com.kgd.news.entity.FrequentlyQuestion;
import com.kgd.news.entity.News;
import com.kgd.news.utils.BaseController;
import com.kgd.news.utils.DateUtils;
import com.kgd.news.utils.StringUtilsWx;

/**
 * @Email: huangsanm@foxmail.com
 * @Date: 2017/9/26
 * @Time: 下午11:12queryForm
 */
public class FrequentlyQuestionController extends BaseController {

    @Override
    public void list() {
        String title = getPara("title");
        String type = "frequently_qusetion";
        String sql = "from frequently_question where 1 = 1";
        if (!StringUtilsWx.isEmpty(title)) {
            sql += " and title like '%" + title + "%'";
            type += title;
        }
//这个要加缓存的
        sql += " order by id desc";
//        Page<FrequentlyQuestion> data=  CacheKit.get("frequently_qusetion", type);//type可以作为缓存的key的吧。
//        if (data == null) {
        Page<FrequentlyQuestion> data = FrequentlyQuestion.dao.paginate(getPageIndex(type), PAGE_SIZE, "select *", sql);
//            CacheKit.put("frequently_qusetion", type, data);
////        }
        setAttr("title", title);
        setAttr("data", data);
        render("frequently_question_list.html");
    }

    @Override
    public void edit() {
        Integer id = getParaToInt("id");
        FrequentlyQuestion dict = FrequentlyQuestion.dao.findById(id);
        setAttr("frequentlyquestion", dict);

        render("frequently_question_edit.html");
    }

    @Override
    public void save() {

        UploadFile uploadFile = getFile("img");
        String filePath = "";
        if (uploadFile != null && !StringUtilsWx.isEmpty(uploadFile.getFileName())) {
            filePath = getFileUploadPath(uploadFile);
        }
        FrequentlyQuestion sd = getModel(FrequentlyQuestion.class, "frequentlyquestion");

        if (sd != null && sd.getId() != null) {
            sd.update();
        } else {
            sd.save();
        }
        CacheKit.removeAll("frequently_qusetion");
        list();
    }

    @Override
    public void del() {
        String ids = getPara("ids");
        Db.update("delete from frequently_question where id in (" + ids + ")");
        CacheKit.removeAll("frequently_question");
        list();
    }

}
